package com.midea.ai.air.ui.more;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.midea.ai.air.R;
import com.midea.ai.air.common.BaseActivity;
import com.midea.ai.air.view.CheckProgressView;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by hww on 2016/4/5.
 * 更多，智能体检
 */
public class MoreSettingCheckbrainpower extends BaseActivity implements View.OnClickListener {
    private RadioButton mBackButton;
    private TextView check_state_tv, check_score_tv, check_unit_tv, start_chack_tv;
    private RelativeLayout start_check, precontract_check, check_grid_rl;
    private CheckProgressView checkProgressView;
    private int mColor;
    private int progress = 0;
    private int Width = 400;
    private Timer timer_check;
    private boolean isChecking;


    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:

                    progress++;
                    if (progress % 75 == 0) {
                        checkProgressView.setProgress(progress, Width, true, mColor);
                    } else {
                        checkProgressView.setProgress(progress, Width, false, mColor);
                    }
                    checkProgressView.invalidate();
                    check_grid_rl.setVisibility(View.VISIBLE);

                    check_state_tv.setText("正在扫描：加湿器");

                    if (progress > 750) {
                        if (timer_check != null) {
                            timer_check.cancel();
                            timer_check = null;
                        }
                        start_chack_tv.setText(R.string.more_setting_check_brainpower_check_start);
                        isChecking = false;
                        check_score_tv.setText("100");
                        check_state_tv.setText("上次体检3天前，建议立即体检");
                        progress = 0;
                        checkProgressView.setProgress(progress, Width, true, mColor);
                    }
                    break;

                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_moresettingcheckbrainpower);

        init();
    }

    private void init() {
        mBackButton = (RadioButton)findViewById(R.id.back_button);
        int width = getResources().getDimensionPixelOffset(R.dimen.height_12dp);
        int height = getResources().getDimensionPixelOffset(R.dimen.height_22dp);
        setDrawableLeft(mBackButton, R.drawable.air_action_bar_left_white, width, height);

        checkProgressView = (CheckProgressView) findViewById(R.id.more_setting_check_progress_view);
        check_state_tv = (TextView) findViewById(R.id.more_setting_check_state_text);
        check_score_tv = (TextView) findViewById(R.id.more_setting_check_score_text);
        check_unit_tv = (TextView) findViewById(R.id.more_setting_check_score_unit_text);
        start_chack_tv = (TextView) findViewById(R.id.more_setting_check_start_check_textView);

        start_check = (RelativeLayout) findViewById(R.id.more_setting_check_start_check);//开始体检
        precontract_check = (RelativeLayout) findViewById(R.id.more_setting_check_precontract);//预约体检
        check_grid_rl = (RelativeLayout) findViewById(R.id.more_setting_check_grid_layout);

        mBackButton.setOnClickListener(this);
        start_check.setOnClickListener(this);
        precontract_check.setOnClickListener(this);

        setCheck_Result();
    }

    /**
     * 设置图标
     *
     * @param radioButton
     * @param drawableId
     * @param width
     * @param height
     */
    public void setDrawableLeft(RadioButton radioButton, int drawableId, int width, int height) {
        Drawable drawable = getResources().getDrawable(drawableId);
        drawable.setBounds(0, 0, width, height);
        radioButton.setCompoundDrawables(drawable, null, null, null);
    }

    private void setCheck_Result() {
        check_score_tv.setText("100");
        check_state_tv.setText("上次体检3天前，建议立即体检");
        mColor = 0;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_button:

                finish();
                break;
            case R.id.more_setting_check_start_check:
                start_CheckDevice();
                break;

            default:
                break;
        }
    }

    //开始体检
    private void start_CheckDevice() {
        if (isChecking) {
            isChecking = false;
            start_chack_tv.setText(R.string.more_setting_check_brainpower_check_start);
            check_score_tv.setText("100");
            check_state_tv.setText("上次体检3天前，建议立即体检");
            progress = 0;
            checkProgressView.setProgress(0, Width, true, mColor);
            if (timer_check != null) {
                timer_check.cancel();
                timer_check = null;
            }
        } else {
            isChecking = true;
            start_chack_tv.setText(R.string.more_setting_check_brainpower_check_start_ing);
            if (timer_check == null) {
                timer_check = new Timer();
                timer_check.schedule(new CheckTimer(), 0, 20);
            }
        }

    }

    class CheckTimer extends TimerTask {

        @Override
        public void run() {
            handler.sendEmptyMessage(0);
        }
    }

}
